//
//  UIImage+RelevantWithImage.m
//  YouYunSDKDemo
//
//  Created by superLeaves on 14/12/23.
//  Copyright (c) 2014年 ucfgroup.com. All rights reserved.
//

#import "UIImage+RelevantWithImage.h"

@implementation UIImage (RelevantWithImage)


/*
 图片显示规则
 1.  图片始终按比例缩放
 2.  长边始终等于126px;36px<=短边<=126px
 3.  1<=长边/短边<=3.5时,长边=126px
 4.  长边/短边>3.5时,短边=72px,长边只显示中间126px的部分
 */
+ (UIImage *)imageShowRegular:(UIImage *)aImage
{
    if (aImage == nil) {
        return nil;
    }
    CGFloat compare = 0.0f;
    CGSize newImageSize = CGSizeMake(0.0, 0.0);
    if (aImage.size.width >= aImage.size.height) {
        compare = aImage.size.width / aImage.size.height;
        if (compare <= 3.5) {
            newImageSize.width = 126.0f;
            newImageSize.height = (aImage.size.height * 126.0) / aImage.size.width;
        }
        else
        {
            newImageSize.height = 36.0f;
            newImageSize.width = (aImage.size.width * 36.0) / aImage.size.height;
        }
    }
    else
    {
        compare = aImage.size.height / aImage.size.width;
        if (compare >= 1.0 && compare <= 3.5) {
            newImageSize.height = 126.0f;
            newImageSize.width = (aImage.size.width * 126.0) / aImage.size.height;
        }
        else
        {
            newImageSize.width = 36.0f;
            newImageSize.height = (aImage.size.height * 36.0) / aImage.size.width;
        }
    }
    UIGraphicsBeginImageContext(newImageSize);
    [aImage drawInRect:CGRectMake(0, 0, newImageSize.width, newImageSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

+ (UIImage *)imageShowOrigin:(UIImage *)aImage
{
    if (aImage == nil) {
        return nil;
    }
    CGSize newImageSize = CGSizeMake(0.0, 0.0);
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    CGFloat compare = (aImage.size.height / aImage.size.width) - screenHeight / screenWidth;
    
    if (compare > 0) {
        newImageSize.height = screenHeight;
        newImageSize.width = (screenHeight*aImage.size.width)/aImage.size.height;
    }
    else
    {
        newImageSize.width = screenWidth;
        newImageSize.height = (screenWidth*aImage.size.height)/aImage.size.width;
    }
    UIGraphicsBeginImageContext(newImageSize);
    [aImage drawInRect:CGRectMake(0, 0, newImageSize.width, newImageSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

+ (UIImage *)fixImageOrientation:(UIImage *)aImage
{
    if (aImage.imageOrientation == UIImageOrientationUp) {
        return aImage;
    }
    CGAffineTransform transform = CGAffineTransformIdentity;
    switch (aImage.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:{
            transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
        }
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:{
            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
        }
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:{
            transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        }
        default:
            break;
    }
    
    switch (aImage.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:{
            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        }
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:{
            transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        }
            
        default:
            break;
    }
    
    CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height, CGImageGetBitsPerComponent(aImage.CGImage), 0, CGImageGetColorSpace(aImage.CGImage), CGImageGetBitmapInfo(aImage.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (aImage.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:{
            CGContextDrawImage(ctx, CGRectMake(0, 0, aImage.size.height, aImage.size.width), aImage.CGImage);
            break;
        }
        default:{
            CGContextDrawImage(ctx, CGRectMake(0, 0, aImage.size.width, aImage.size.height), aImage.CGImage);
            break;
        }
    }
    
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
    
    
    
}

@end
